#pragma once
// #include "Calculator.hpp"
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>
#include"Calculator.hpp"
const std::string Seq = "\r\n";
Calculator _cal;
bool Encode(std::string &message) // 添加报头
{
    if (message.empty())
    {
        return false;
    }
    std::string pack = std::to_string(message.size()) + Seq + message + Seq;
    message = pack;
    return true;
}
//  len+\r\n+message+\r\n
bool Decode(std::string &message, std::string *out) // 去除报头
{
    if (message.empty())
        return false;
    auto pos = message.find(Seq);
    if (pos == std::string::npos)
        return false;
    int len = stoi(message.substr(0, pos));
    if (len + Seq.size() * 2 + pos < message.size())
    {
        return false;
    }
    *out = message.substr(pos + Seq.size(), len);
    //注意正文可能为空串
    message.erase(0, len + Seq.size() * 2 + pos);
    return true;
}
class Request
{
public:
    Request(int x, int y, const char op)
        : _x(x), _y(y), _op(op) {};
    Request() {}
    ~Request() {}
    bool Serialize(std::string &out)
    {
        Json::Value root;
        root["_x"] = _x;
        root["_y"] = _y;
        root["_op"] = _op;

        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
        std::stringstream ss;
        writer->write(root, &ss);
        out = ss.str();
        return true;
    }
    bool Deserialize(std::string &in)
    {
       
        Json::Value root;
        Json::Reader reader;
        bool parese_success = reader.parse(in, root);
        if (!parese_success)
        {
            std::cout << "Json parse error" << reader.getFormattedErrorMessages() << std::endl;
            return false;
        }
        _x = root["_x"].asInt();
        _y = root["_y"].asInt();
        _op = root["_op"].asInt();
        std::cout<<"aaabbbbbbbbbbbbbbbbbbbbbb"<<"\n";
        return true;
    }
    void Print()
    {
        std::cout << "Request: x=" << _x << " y=" << _y << " op=" << _op << std::endl;
    }
    int X()
    {
        return _x;
    }
    int Y()
    {
        return _y;
    }
    char Op()
    {
        return _op;
    }

private:
    int _x, _y;
    char _op;
};

class Response
{
public:
    Response() {}
    Response(double result, int error_code)
        : _result(result), _error_code(error_code) {};
    bool Serialize(std::string &out)
    {
        Json::Value root;
        root["_result"] = _result;
        root["_error_code"] = _error_code;
        Json::StreamWriterBuilder builder;
        std::unique_ptr<Json::StreamWriter> Writer(builder.newStreamWriter());
        std::stringstream ss;
        Writer->write(root, &ss);
        out = ss.str();
        return true;
    }
    bool Deserialize(std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool parse_success = reader.parse(in, root);
        _result = root["_result"].asDouble();
        _error_code = root["_error_code"].asInt();
        return true;
    }
    double Result()
    {
        return _result;
    }
    int ErrorCode()
    {
        return _error_code;
    }
    ~Response() {};
    void SetResult(double result)
    {
        _result = result;
    }
    void SetErrorCode(int error_code)
    {
        _error_code = error_code;
        }
private:
    double _result;
    int _error_code;
};
std::string HandlerRequese(std::string&inbuff)
{
    std::string request;
    std::string all_response;
while(Decode(inbuff,&request))
{
    //反序列化
    if(request.empty())//正文为空我们规定认为可以结束了
    {
        break;
    }
    Request req;
    if(req.Deserialize(request)==0)
    break;
    Response rep=_cal.Execute(req);
    std::string response; 
    rep.Serialize(response);
    Encode(response);
    all_response+=response;
}
return all_response.empty()?"None":all_response;
}